package com.qf.set.controller;

import com.qf.common.annotation.Log;
import com.qf.common.enums.BusinessType;
import com.qf.set.domain.TmsRole;
import com.qf.set.service.ITmsRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.qf.common.utils.PageUtils.startPage;

/**
 * --- 天道酬勤 ---
 *
 * @author 卢泓光
 * @date 2025/9/3
 * @desc
 */
@Slf4j
@RestController
@RequestMapping("/tms/set")
public class TmsRoleController {

    @Autowired
    private ITmsRoleService roleService;

    /**
     * 显示角色列表
     * 前端发出了分页的数据param
     * @return
     */
    @RequestMapping("/role/list")
    public List<TmsRole> roleList(TmsRole tmsRole) {
        startPage(); // 这是若依封装的工具类，自动从请求参数获取pageNum和pageSize
        //获取前端传到后端的数据
        List<TmsRole> list = roleService.roleList(tmsRole);
        System.out.println(list);
        return list;
    }
    /**
     * 新增角色
     * @param tmsRole
     * @return
     * post
     */
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    public int addRole(@RequestBody TmsRole tmsRole) {
        return roleService.insertRole(tmsRole);
    }
    /**
     * 更新角色状态
     * @param params 包含roleId和status的参数对象
     * @return 影响行数
     */
    @RequestMapping(value = "/role/changeStatus", method = RequestMethod.PUT)
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    public int changeRoleStatus(@RequestBody Map<String, Object> params) {
        Integer roleId = (Integer) params.get("roleId");
        String status = (String) params.get("status");
        return roleService.updateRoleStatus(roleId, status);
    }
    /**
     * 删除角色
     * @param roleId 角色ID
     * @return 影响行数
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/role/{roleId}", method = RequestMethod.DELETE)
    public int deleteRole(@PathVariable Integer roleId) {
        return roleService.deleteRole(roleId);
    }
    
    /**
     * 更新角色信息
     * @param tmsRole 角色信息
     * @return 影响行数
     */
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/role", method = RequestMethod.PUT)
    public int updateRole(@RequestBody TmsRole tmsRole) {
        return roleService.updateRole(tmsRole);
    }
}